/**
 * SqlTables.java
 * Sql script parser; Parse the passed sql script and execute
 * the required commands to create the Library Manager DB,
 * and populate it with default records.
 *
 * @author:  John Thomason
 *  Course:  CS223
 *  Assign:  Final Exam
 * Seminar:  6
 *    Date:  Feb 16, 2009
 */

// Assigned Package
package libman.db;

// Package Imports
import java.sql.*;
import java.io.*;
import java.util.*;

/**
 * SqlTables Class
 */
public class SqlTables
{
    //Constructors
    private SqlTables(){}

    public static void construct(Connection dbConn, File sqlScript)
    throws SQLException 
    {
        try {
            String qStr    = new String();
            Statement stmt = dbConn.createStatement();
            Scanner script = new Scanner(sqlScript);
            
            while (script.hasNextLine()){

                String buffer = script.nextLine().trim();
                if (!buffer.startsWith("--")){  //Skip Comments
                    
                    qStr += buffer;
                    if (qStr.endsWith(";")){    //End of SQL Command
                        //strip the ; at the end of command,
                        //the jdbc driver handles command termination
                        qStr = qStr.substring(0, qStr.length() - 1);
                        stmt.execute(qStr);
                        qStr = new String();
                    }//if end of qStr statement
                    
                }//if not a comment

            }//while

        }//try
        catch (Exception e){
            throw new SQLException("SqlTables.construct::" +
                                   "Can't Create DB Tables::" +
                                    e.getMessage());
        }//catch

    }//construct()

}//SqlTables Class
